package com.travel.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.travel.modules.sys.dao.SysUserDao;
import com.travel.modules.sys.dao.SysUserTokenDao;
import com.travel.modules.sys.entity.SysUser;
import com.travel.modules.sys.entity.SysUserToken;
import com.travel.modules.sys.service.ShiroService;
import com.travel.modules.sys.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service("ShiroService")
public class ShiroServiceImpl implements ShiroService {
    @Resource
    private SysUserDao sysUserDao;

    @Resource
    private SysUserTokenDao sysUserTokenDao;

    @Override
    public Set<String> getUserPermissions(Integer userId) {
        List<String> permsList = sysUserDao.queryAllPerms(userId);

        Set<String> permsSet = new HashSet<>();
        for (String perms : permsList) {
            if (StringUtils.isBlank(perms)) {
                continue;
            }
            permsSet.addAll(Arrays.asList(perms.trim().split(",")));
        }
        return permsSet;
    }

    @Override
    public SysUserToken queryByToken(String token) {
        return sysUserTokenDao.selectOne(new QueryWrapper<SysUserToken>().eq("token",token));
    }

    @Override
    public SysUser queryUser(Integer userId) {
        return sysUserDao.selectById(userId);
    }
}
